﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_Link_Operation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AdminCs.CheckLoginState();
        if (!this.IsPostBack)
        {
            if (Request.QueryString["lk"] == "edit")
            {
                link_txt();
            }
            else if (Request.QueryString["lk"] == "del")
            {
                link_del();
            }
        }
    }
    private void link_del()
    {
        int id = int.Parse(Request.QueryString["ID"]);
        string adSql = "delete from Link where link_ID=" + id;
        Class1.ExecSql(adSql);
        Class1.AlertShow("删除成功！", "Link_Manage.aspx");
    }
    private void link_txt()
    {
        int id = int.Parse(Request.QueryString["ID"]);
        DataTable dt = Class1.ExecSel("select * from Link where link_ID='" + id + "'");
        txtProImg.Text = Convert.ToString(dt.Rows[0]["link_Img"]);
        txtUrl.Text = Convert.ToString(dt.Rows[0]["link_Url"]);
        if (dt.Rows[0]["link_Lock"].ToString() == "1")
        {
            CheckBox1.Checked = true;
        }
        else
        {
            CheckBox1.Checked = false;
        }
    }
    private void link_edit()
    {
        int id = int.Parse(Request.QueryString["ID"]);
        string link_Lock;
        string link_Img = txtProImg.Text;
        string link_Url = txtUrl.Text;
        if (this.CheckBox1.Checked)
        {
            link_Lock = "1";

        }
        else
        {

            link_Lock = "0";
        }
        if (link_Img == "")
        {
            Class1.AlertShow("请上传图片！");
            return;
        }
        string sql = ("update Link set link_Img='" + link_Img + "',link_Url='" + link_Url + "',link_Lock='" + link_Lock + "' where link_ID=" + id + "");
        Class1.ExecSql(sql);
        Class1.AlertShow("修改成功！", "Link_Manage.aspx");
    }
    protected void UpImg_Click(object sender, EventArgs e)//上传图片
    {
        if (FileUpload1.PostedFile.ContentLength < 500000)//100000为100K
        {

            string fileFullname = this.FileUpload1.FileName;
            string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
            string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
            string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
            if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
            {
                this.FileUpload1.SaveAs(Server.MapPath("../Images") + "\\" + dataName + "." + type);
                string ProImg = "Images/" + dataName + "." + type;
                this.txtProImg.Text = "Images/" + dataName + "." + type;
            }
            else
            {
                Response.Write("<script language='javascript'>alert('支持格式：|jpg|gif|bmp|');</script>");
            }
        }
        else
        {
            Response.Write("<script language='javascript'>alert('你的图片已经超过500K的大小！');</script>");
        }
    }
    private void link_add()
    {
        string link_Lock;
        string link_Img = txtProImg.Text;
        string link_Url = txtUrl.Text;
        if (this.CheckBox1.Checked)
        {
            link_Lock = "1";

        }
        else
        {

            link_Lock = "0";
        }
        if (link_Img == "")
        {
            Class1.AlertShow("请上传图片！");
            return;
        }
        string sql = "insert into Link(link_Img,link_Url,link_Lock)values('" + link_Img + "','" + link_Url + "','" + link_Lock + "')";
        Class1.ExecSql(sql);
        Class1.AlertShow("添加成功！", "Link_Operation.aspx?lk=add");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["lk"] == "add")
        {

            link_add();
        }
        else if (Request.QueryString["lk"] == "edit")
        {

            link_edit();
        }
    }
}
